package edu.calpoly.android.echoes;


public class Vector2D {
   private float xDir, yDir;
   
   public Vector2D(){
	   xDir = 0;
	   yDir = 0;
   }
   
   public Vector2D(float x, float y){
	   xDir = x;
	   yDir = y;
   }
   
   public void normalize(){
	   float magnitude = calculateMagnitude();
	   if(magnitude < .99997 || magnitude > 1.00003) {
		   xDir /= magnitude;
		   yDir /= magnitude;
		   magnitude = 1;
	   }
   }
   
   public float getX(){
	   return xDir;
   }
   
   public float getY(){
	   return yDir;
   }
   
   public void setX(float x){
	   xDir = x;
   }
   
   public void setY(float y){
	   yDir = y;
   }
   
   public void set(float x,float y){
	   xDir = x;
	   yDir = y;
   }
   
   public float calculateMagnitude(){
	   return (float) (Math.sqrt((xDir*xDir) + (yDir*yDir)));
   }
   
   public Vector2D minus(Vector2D v1, Vector2D v2) {
	   return new Vector2D((v1.xDir - v2.xDir),
			               (v1.yDir - v2.yDir));
   }
   
   public Vector2D plus(Vector2D v1, Vector2D v2) {
	   return new Vector2D((v1.xDir + v2.xDir),
			               (v1.yDir + v2.yDir));
   }
   
   public Vector2D multiply(Vector2D v1, float scalar) {
	   return new Vector2D((v1.xDir*scalar),
			               (v1.yDir*scalar));
   }
   
   public float dotProduct(Vector2D v1, Vector2D v2) {
	   return ((v1.xDir*v2.xDir) + (v1.yDir*v2.yDir));
   }
   
   
   //r = v - 2 * n * (n DOT v)
   public Vector2D getReflection(Vector2D v, Vector2D normal) {
	   //v.normalize();
	   //normal.normalize();
	   
	   float nDotV = dotProduct(normal, v); 
	   
	   Vector2D r = minus(v, (multiply(normal,(2*nDotV))));
	   
	   //r.normalize();
	   return r;
   }
   
   public float getAngle(Vector2D v, Vector2D normal) {
	   return (float) 1.0;
   }
   
   public String toString(){
	   return "Vector2D = (" + xDir + "," + yDir + ")";
   }
}


